//copilot begin
package com.codingfly;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Service
public class DemoService {

    @Resource(name = "orderJdbcTemplate")
    public JdbcTemplate orderJdbcTemplate;

    @Resource(name = "orderNamedJdbcTemplate")
    public NamedParameterJdbcTemplate orderNamedJdbcTemplate;

    @Resource(name = "kucunJdbcTemplate")
    public JdbcTemplate kucunJdbcTemplate;

    @Resource(name = "kucunNamedJdbcTemplate")
    public NamedParameterJdbcTemplate kucunNamedJdbcTemplate;

    @Resource(name = "fundJdbcTemplate")
    public JdbcTemplate fundJdbcTemplate;

    @Resource(name = "fundNamedJdbcTemplate")
    public NamedParameterJdbcTemplate fundNamedJdbcTemplate;

    public ObjectMapper objectMapper = new ObjectMapper();

    public void test() throws JsonProcessingException {
        List<Map<String, Object>> mapList = orderJdbcTemplate.queryForList("select version();");
        System.out.println(objectMapper.writeValueAsString(mapList));

        mapList = orderNamedJdbcTemplate.queryForList("select version();", new HashMap<>());
        System.out.println(objectMapper.writeValueAsString(mapList));

        mapList = kucunJdbcTemplate.queryForList("select version();");
        System.out.println(objectMapper.writeValueAsString(mapList));

        mapList = kucunNamedJdbcTemplate.queryForList("select version();", new HashMap<>());
        System.out.println(objectMapper.writeValueAsString(mapList));

        mapList = fundJdbcTemplate.queryForList("select version();");
        System.out.println(objectMapper.writeValueAsString(mapList));

        mapList = fundNamedJdbcTemplate.queryForList("select version();", new HashMap<>());
        System.out.println(objectMapper.writeValueAsString(mapList));

        insertOrderDB();

        insertKucunDB();

        insertFundDB();

    }

    @Transactional("orderTransactionManager")
    public void insertOrderDB() {
        String sql = "INSERT INTO user(username,password) values (:username, :password);";
        MapSqlParameterSource params = new MapSqlParameterSource()
                .addValue("username", UUID.randomUUID().toString().replace("-",""))
                .addValue("password", UUID.randomUUID().toString().replace("-",""));
        orderNamedJdbcTemplate.update(sql, params);
    }

    @Transactional("kucunTransactionManager")
    public void insertKucunDB() {
        String sql = "INSERT INTO user(username,password) values (:username, :password);";
        MapSqlParameterSource params = new MapSqlParameterSource()
                .addValue("username", UUID.randomUUID().toString().replace("-",""))
                .addValue("password", UUID.randomUUID().toString().replace("-",""));
        kucunNamedJdbcTemplate.update(sql, params);
    }

    @Transactional("fundTransactionManager")
    public void insertFundDB() {
        String sql = "INSERT INTO user(username,password) values (:username, :password);";
        MapSqlParameterSource params = new MapSqlParameterSource()
                .addValue("username", UUID.randomUUID().toString().replace("-",""))
                .addValue("password", UUID.randomUUID().toString().replace("-",""));
        fundNamedJdbcTemplate.update(sql, params);
    }

}
//copilot end